import { Course, Student, Teacher, Admin } from '../types';

// Mock 学生信息
export const mockStudent: Student = {
  id: 'student-001',
  name: '张明',
  college: '计算机科学与技术学院',
  major: '软件工程',
  clazz: '软件2101班',
  studentNo: '2021012345',
  email: 'zhangming@example.com',
  phone: '13800138000',
  grade: '2021级',
  selectedCount: 2,
  totalCredits: 7,
  requiredCredit: 20, // 本学期所需学分
};

// Mock 教师信息
export const mockTeacher: Teacher = {
  id: 'teacher-001',
  name: '张教授',
  college: '计算机学院',
  account: 'teacher1',
  title: '教授',
  email: 'zhang@university.edu.cn',
  phone: '13800138000',
  researchArea: '数据库结构与算法、人工智能',
  publishedPapers: 25,
  intro: '张教授,计算机学院教授,博士生导师。主要研究方向为数据库结构与算法、人工智能。',
};

// Mock 管理员信息
export const mockAdmin: Admin = {
  id: 'admin-001',
  name: '管理员',
  account: 'ADMIN001',
  email: 'admin@example.com',
  phone: '13900139000',
  lastLogin: '2023-10-08 09:23:45',
};

// Mock 学生列表
export const mockStudents: Student[] = [
  {
    id: 'student-001',
    name: '张明',
    college: '计算机科学与技术学院',
    major: '软件工程',
    clazz: '软件2101班',
    studentNo: '2021012345',
    email: 'zhangming@example.com',
    phone: '13800138000',
    grade: '2021级',
  },
  {
    id: 'student-002',
    name: '李华',
    college: '计算机科学与技术学院',
    major: '计算机科学与技术',
    clazz: '计科2102班',
    studentNo: '2021012346',
    email: 'lihua@example.com',
    phone: '13800138001',
    grade: '2021级',
  },
  {
    id: 'student-003',
    name: '王芳',
    college: '计算机科学与技术学院',
    major: '人工智能',
    clazz: '智能2101班',
    studentNo: '2021012347',
    email: 'wangfang@example.com',
    phone: '13800138002',
    grade: '2021级',
  },
  {
    id: 'student-004',
    name: '刘强',
    college: '信息工程学院',
    major: '电子信息工程',
    clazz: '电子2101班',
    studentNo: '2021021234',
    email: 'liuqiang@example.com',
    phone: '13800138003',
    grade: '2021级',
  },
  {
    id: 'student-005',
    name: '陈静',
    college: '信息工程学院',
    major: '通信工程',
    clazz: '通信2101班',
    studentNo: '2021021235',
    email: 'chenjing@example.com',
    phone: '13800138004',
    grade: '2021级',
  },
];

// Mock 教师列表
export const mockTeachers: Teacher[] = [
  {
    id: 'teacher-001',
    name: '张教授',
    college: '计算机学院',
    account: 'teacher1',
    title: '教授',
    email: 'zhang@university.edu.cn',
    phone: '13800138000',
    researchArea: '数据库结构与算法、人工智能',
    publishedPapers: 25,
  },
  {
    id: 'teacher-002',
    name: '李教授',
    college: '计算机学院',
    account: 'teacher2',
    title: '教授',
    email: 'li@university.edu.cn',
    phone: '13800138010',
    researchArea: '机器学习、深度学习',
    publishedPapers: 30,
  },
  {
    id: 'teacher-003',
    name: '王老师',
    college: '计算机学院',
    account: 'teacher3',
    title: '副教授',
    email: 'wang@university.edu.cn',
    phone: '13800138020',
    researchArea: '计算机网络、分布式系统',
    publishedPapers: 15,
  },
  {
    id: 'teacher-004',
    name: '刘老师',
    college: '信息工程学院',
    account: 'teacher4',
    title: '讲师',
    email: 'liu@university.edu.cn',
    phone: '13800138030',
    researchArea: '嵌入式系统、物联网',
    publishedPapers: 8,
  },
];


// Mock 课程数据（已审批的课程）
export const mockCourses: Course[] = [
  {
    id: 'course-001',
    name: '数据结构与算法',
    teacherName: '张教授',
    credit: 3,
    capacity: 120,
    enrolledCount: 85,
    timeSlots: [{ dayOfWeek: 1, start: '14:00', end: '16:00', room: 'A101', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-002',
    name: '大学物理(上)',
    teacherName: '李教授',
    credit: 4,
    capacity: 150,
    enrolledCount: 92,
    timeSlots: [{ dayOfWeek: 2, start: '09:00', end: '11:00', room: 'B201', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-003',
    name: '中国现代文学',
    teacherName: '王教授',
    credit: 2,
    capacity: 80,
    enrolledCount: 45,
    timeSlots: [{ dayOfWeek: 3, start: '14:00', end: '15:30', room: 'C301', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-004',
    name: '人工智能导论',
    teacherName: '赵教授',
    credit: 3,
    capacity: 100,
    enrolledCount: 100,
    timeSlots: [{ dayOfWeek: 4, start: '10:00', end: '12:00', room: 'D401', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-005',
    name: '计算机网络',
    teacherName: '刘教授',
    credit: 3,
    capacity: 90,
    enrolledCount: 67,
    timeSlots: [{ dayOfWeek: 5, start: '14:00', end: '16:00', room: 'E501', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-006',
    name: '数据库系统',
    teacherName: '陈教授',
    credit: 3,
    capacity: 110,
    enrolledCount: 78,
    timeSlots: [{ dayOfWeek: 1, start: '10:00', end: '12:00', room: 'F601', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-007',
    name: '操作系统',
    teacherName: '周教授',
    credit: 4,
    capacity: 95,
    enrolledCount: 88,
    timeSlots: [{ dayOfWeek: 2, start: '14:00', end: '16:00', room: 'G701', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-008',
    name: '软件工程',
    teacherName: '吴教授',
    credit: 3,
    capacity: 85,
    enrolledCount: 52,
    timeSlots: [{ dayOfWeek: 3, start: '09:00', end: '11:00', room: 'H801', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-009',
    name: '编译原理',
    teacherName: '孙教授',
    credit: 4,
    capacity: 70,
    enrolledCount: 58,
    timeSlots: [{ dayOfWeek: 4, start: '14:00', end: '16:00', room: 'I901', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-010',
    name: '计算机图形学',
    teacherName: '马教授',
    credit: 3,
    capacity: 60,
    enrolledCount: 35,
    timeSlots: [{ dayOfWeek: 5, start: '10:00', end: '12:00', room: 'J1001', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-011',
    name: 'Web前端开发',
    teacherName: '钱教授',
    credit: 2,
    capacity: 100,
    enrolledCount: 72,
    timeSlots: [{ dayOfWeek: 1, start: '08:00', end: '09:30', room: 'K1101', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-012',
    name: '移动应用开发',
    teacherName: '郑教授',
    credit: 3,
    capacity: 75,
    enrolledCount: 41,
    timeSlots: [{ dayOfWeek: 6, start: '14:00', end: '16:00', room: 'L1201', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-013',
    name: '信息安全基础',
    teacherName: '冯教授',
    credit: 2,
    capacity: 90,
    enrolledCount: 63,
    timeSlots: [{ dayOfWeek: 2, start: '08:00', end: '09:30', room: 'M1301', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-014',
    name: '软件测试',
    teacherName: '徐教授',
    credit: 2,
    capacity: 80,
    enrolledCount: 49,
    timeSlots: [{ dayOfWeek: 3, start: '14:00', end: '15:30', room: 'N1401', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-015',
    name: 'Java程序设计',
    teacherName: '何教授',
    credit: 3,
    capacity: 120,
    enrolledCount: 96,
    timeSlots: [{ dayOfWeek: 4, start: '09:00', end: '11:00', room: 'O1501', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-016',
    name: 'Python编程基础',
    teacherName: '张教授',
    credit: 2,
    capacity: 100,
    enrolledCount: 82,
    timeSlots: [{ dayOfWeek: 5, start: '14:00', end: '15:30', room: 'P1601', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-017',
    name: 'C++程序设计',
    teacherName: '李教授',
    credit: 3,
    capacity: 110,
    enrolledCount: 95,
    timeSlots: [{ dayOfWeek: 1, start: '08:00', end: '10:00', room: 'Q1701', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-018',
    name: '机器学习',
    teacherName: '王老师',
    credit: 3,
    capacity: 90,
    enrolledCount: 68,
    timeSlots: [{ dayOfWeek: 2, start: '10:00', end: '12:00', room: 'R1801', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-019',
    name: '深度学习',
    teacherName: '刘老师',
    credit: 3,
    capacity: 75,
    enrolledCount: 55,
    timeSlots: [{ dayOfWeek: 3, start: '14:00', end: '16:00', room: 'S1901', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-020',
    name: '数据挖掘',
    teacherName: '周教授',
    credit: 2,
    capacity: 85,
    enrolledCount: 62,
    timeSlots: [{ dayOfWeek: 4, start: '14:00', end: '15:30', room: 'T2001', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
  {
    id: 'course-021',
    name: '云计算技术',
    teacherName: '陈教授',
    credit: 3,
    capacity: 95,
    enrolledCount: 77,
    timeSlots: [{ dayOfWeek: 5, start: '08:00', end: '10:00', room: 'U2101', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-022',
    name: '大数据技术',
    teacherName: '吴教授',
    credit: 3,
    capacity: 105,
    enrolledCount: 89,
    timeSlots: [{ dayOfWeek: 6, start: '09:00', end: '11:00', room: 'V2201', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
];

// Mock 已选课程（初始状态，可以修改）
export let mockMyCourses: Course[] = [
  {
    id: 'course-001',
    name: '数据结构与算法',
    teacherName: '张教授',
    credit: 3,
    capacity: 120,
    enrolledCount: 85,
    timeSlots: [{ dayOfWeek: 1, start: '14:00', end: '16:00', room: 'A101', weeks: [1,2,3,4,5,6,7,8,9,10], startWeek: 1, endWeek: 10 }],
  },
  {
    id: 'course-005',
    name: '计算机网络',
    teacherName: '刘教授',
    credit: 3,
    capacity: 90,
    enrolledCount: 67,
    timeSlots: [{ dayOfWeek: 5, start: '14:00', end: '16:00', room: 'E501', weeks: [11,12,13,14,15,16,17,18,19,20], startWeek: 11, endWeek: 20 }],
  },
];

// Mock 选课函数（模拟选课操作）
export function mockSelectCourse(courseId: string): Promise<void> {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const course = mockCourses.find(c => c.id === courseId);
      if (!course) {
        reject(new Error('课程不存在'));
        return;
      }
      if (mockMyCourses.some(c => c.id === courseId)) {
        reject(new Error('已选择该课程'));
        return;
      }
      if (course.enrolledCount >= course.capacity) {
        reject(new Error('课程容量已满'));
        return;
      }
      // 检查时间冲突
      const hasConflict = mockMyCourses.some(myCourse => {
        return myCourse.timeSlots.some(mySlot => {
          return course.timeSlots.some(slot => {
            if (mySlot.dayOfWeek !== slot.dayOfWeek) return false;
            const myStart = timeToMinutes(mySlot.start);
            const myEnd = timeToMinutes(mySlot.end);
            const slotStart = timeToMinutes(slot.start);
            const slotEnd = timeToMinutes(slot.end);
            return Math.max(myStart, slotStart) < Math.min(myEnd, slotEnd);
          });
        });
      });
      if (hasConflict) {
        reject(new Error('与已选课程时间冲突'));
        return;
      }
      // 选课成功
      mockMyCourses.push(course);
      course.enrolledCount++;
      resolve();
    }, 500);
  });
}

// Mock 退选函数
export function mockDropCourse(courseId: string): Promise<void> {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const index = mockMyCourses.findIndex(c => c.id === courseId);
      if (index === -1) {
        reject(new Error('未选择该课程'));
        return;
      }
      const course = mockCourses.find(c => c.id === courseId);
      if (course) {
        course.enrolledCount--;
      }
      mockMyCourses.splice(index, 1);
      resolve();
    }, 500);
  });
}

function timeToMinutes(time: string): number {
  const [h, m] = time.split(':').map(Number);
  return h * 60 + m;
}

// 兼容旧版本导出（教师端使用）
const dayNames = ['', '一', '二', '三', '四', '五', '六', '日'];
export const courses = mockCourses.map(c => ({
  ...c,
  teacherId: 'teacher-001', // 默认教师ID，可以根据实际需求修改
  time: c.timeSlots.map(t => `周${dayNames[t.dayOfWeek]} ${t.start}-${t.end}`).join(', '), // 转换为教师端需要的格式
}));

